//
//  FileData.swift
//  Trans
//
//  Created by zcj on 2024/11/18.
//
import Combine
import Foundation
class FileData:BaseData{
    var fileName:String = ""
    var fileData:Data = Data()
    var fileId:FileId = 0
    
    init(fileName: String, fileData: Data) {
        self.fileName = fileName
        self.fileData = fileData
        super.init()
    }
    init(fileId:FileId,fileName: String, fileData: Data){
        self.fileId = fileId
        self.fileName = fileName
        self.fileData = fileData
        super.init()
    }
    
    enum CodingKeys: String, CodingKey{
        case fileName
        case fileData
        case fileId
    }
    
    //从json转化为类的对象
    required init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy:CodingKeys.self)
        fileName = try container.decode(String.self, forKey: .fileName)
        fileData = try container.decode(Data.self, forKey: .fileData)
        fileId = try container.decode(FileId.self, forKey: .fileId)
        try super.init(from: decoder)
    }
    
    //json化
    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(fileName, forKey: .fileName)
        try container.encode(fileData, forKey: .fileData)
        try container.encode(fileId, forKey: .fileId)
        
        try super.encode(to: encoder)
    }
    
    override func getType() -> UINT32 {
        return FILE_DATA;
    }
}
